/*
*
* ScaleUI
*
* This class implements a scale for intepreting neighbourhood shading
*
*/
class ScaleUI {
  
  // -------------------------------------
  // Attributes
  // -------------------------------------
  ControlP5 controlp5;
  PApplet parent;
  float origX, origY, wid, hei;    // dimensions for the scale
  float[][] maxMins;               // array of maxMins data for various metrics
  int divs = 30;                   // number of scale divisions
  float val;
  Textlabel maxLabel, minLabel, noDataLabel, displayTitle, modeLabel;
  String ma = "";
  String mi = "";
  String[] dModeNames;
  
  // -------------------------------------
  // Constructor
  // -------------------------------------
  ScaleUI(ControlP5 cp5, PApplet p55, float[][] mm, String[] dmn, float x, float y, float w, float h) {
    
    // set attributes
    
    controlp5 = cp5;
    parent = p55;
    maxMins = mm;
    dModeNames = dmn;
    origX = x;
    origY = y;
    wid = w;
    hei = h;
    
    // create scale labels
    
    maxLabel = controlp5.addTextlabel("maxLabel", ma, int(origX + wid + 4), int(origY + 2));
    maxLabel.setColorValue(0);
    maxLabel.hide(); // drawn during draw loop
    minLabel = controlp5.addTextlabel("minLabel", mi, int(origX + wid + 4), int(origY + hei - 8));
    minLabel.setColorValue(0);
    minLabel.hide(); // drawn during draw loop
    minLabel.setColorBackground(0);
    noDataLabel = controlp5.addTextlabel("noDataLabel", "no data", int(origX + wid + 4), int(origY + hei + 5));
    noDataLabel.setColorValue(0);
    noDataLabel.hide(); // drawn during draw loop
    noDataLabel.setColorBackground(0);
    
    // create drawing mode labels
    displayTitle = controlp5.addTextlabel("displayTitle", "displaying:", int(origX + wid + 50), int(origY + 2));
    displayTitle.setColorValue(0);
    displayTitle.hide(); // drawn during draw loop
    displayTitle.setColorBackground(0);
    modeLabel = controlp5.addTextlabel("modeLabel", dModeNames[dMode], int(origX + wid + 50), int(origY + 15));
    modeLabel.setColorValue(0);
    modeLabel.hide(); // drawn during draw loop
    modeLabel.setColorBackground(0);
    
  } // constructor
  
  // -------------------------------------
  // Draw
  // draws scale
  // -------------------------------------
  void draw(int dMode, ColourManager colourMan, boolean pdf) {
    
    // draw "frame" rectangle; if pdf, add stroke, fill white, and narrow
    if(pdf) {
      fill(255);
      stroke(150);
      rect((origX - 4), (origY - 4), 190, (hei + 20));
    } else {
      noStroke();
      fill(50, 50);
      rect((origX - 4), (origY - 4), 225, (hei + 20));
    }
    
    color col;
    noStroke();
    
    // check to make sure drawing mode needs scale drawn
    if(dMode == 0) {
      
      // hide scale labels if mode doesn't require a scale
      minLabel.hide();
      maxLabel.hide();
      noDataLabel.hide();
      
      // show display title (ie label that says "displaying")
      displayTitle.show();
      
      // refresh mode label
      modeLabel.setValue(dModeNames[dMode]);
      modeLabel.show();
      
    } else {
      
      // draw scale
      
      for(int i = 0; i < divs; i++) {
        
        // draw coloured portion
        val = maxMins[dMode][1] - (i + 0.5) * (maxMins[dMode][1] - maxMins[dMode][0]) / divs;
        col = colourMan.getColour(dMode, maxMins[dMode][0], maxMins[dMode][1], val);
        fill(col);
        rect(origX, (origY + hei / divs * i), wid, (hei / divs));
        
        // draw "no data" square
        fill(noDataFil[0], noDataFil[1], noDataFil[2]);
        rect(origX, (origY + hei + 5), wid, 8);
        
      } // for
      
      // draw labels
      
      // update values
      mi = nf(maxMins[dMode][0], 3, 1);
      ma = nf(maxMins[dMode][1], 3, 1);
      minLabel.setValue(mi);
      maxLabel.setValue(ma);
      modeLabel.setValue(dModeNames[dMode]);
      
      // draw labels if writing to pdf; show if not
      if(pdf) {
        minLabel.draw(parent);
        maxLabel.draw(parent);
        noDataLabel.draw(parent);
        displayTitle.draw(parent);
        modeLabel.draw(parent);
      } else {
        // show labels
        minLabel.show();
        maxLabel.show();
        noDataLabel.show();
        displayTitle.show();
        modeLabel.show();
      }
    } // if
  } // draw method
  
} // Scale UI class
